
using UnityEngine;
using UnityEngine.UI;

public class PopUpText : MonoBehaviour
{
    [SerializeField] private float moveSpeed;
    [SerializeField] private float disappearSpeed;
    [SerializeField] private float colorDisappearSpeed;
    [SerializeField] private float lifeTime;
    private float lifeTimer;
    private Text myText;

    // Start is called before the first frame update
    void Start()
    {
        lifeTimer = lifeTime;   
        myText = GetComponent<Text>();

    }

    // Update is called once per frame
    void Update()
    {
        lifeTimer -= Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, new Vector3(transform.position.x, transform.position.y + 1, transform.position.z), moveSpeed * Time.deltaTime);

        if(lifeTimer < 0)
        {
            float alpha = myText.color.a - colorDisappearSpeed * Time.deltaTime;
            myText.color = new Color(myText.color.r, myText.color.g, myText.color.b, alpha);

            if (myText.color.a < 50)
            {
                moveSpeed = disappearSpeed;
            }
            if(myText.color.a <= 0)
            {
                Destroy(gameObject);
            }

        }
    }
}
